home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 23 / AMIGAplus Sonderheft 23 (2000)(Falke)(DE)[!].iso / Updates / Datatypes / RGFX-DT35 / Source / dispatcher.c < prev    next >
C/C++ Source or Header  |  1999-11-14  |  15KB  |  648 lines

  1. #include <clib/alib_protos.h>
  2. #include <pragma/datatypes_lib.h>
  3. #include <pragma/dos_lib.h>
  4. #include <pragma/exec_lib.h>
  5. #include <pragma/graphics_lib.h>
  6. #include <pragma/iffparse_lib.h>
  7. #include <pragma/intuition_lib.h>
  8. #include <pragma/utility_lib.h>
  9. #include <pragma/xpkmaster_lib.h>
  10. #include <datatypes/pictureclass.h>
  11. #include <exec/memory.h>
  12. #include <intuition/icclass.h>
  13. #include <string.h>
  14. #include "class.h"
  15. #include "rgfx.h"
  16.  
  17. struct Arg1
  18. {
  19. char *xpk;
  20. long *xpkmode;
  21. char *password;
  22. };
  23.  
  24. ULONG SaveRGFX(IClass *cl,Object *obj,dtWrite *msg);
  25.  
  26. static long iff2dos[]=
  27. {
  28. 0,
  29. 0,
  30. DTERROR_INVALID_DATA,
  31. ERROR_NO_FREE_STORE,
  32. ERROR_SEEK_ERROR,
  33. ERROR_SEEK_ERROR,
  34. ERROR_SEEK_ERROR,
  35. DTERROR_INVALID_DATA,
  36. DTERROR_INVALID_DATA,
  37. ERROR_OBJECT_WRONG_TYPE,
  38. ERROR_REQUIRED_ARG_MISSING,
  39. 0xDEADDEAD
  40. };
  41.  
  42. static long chunks[]=
  43. {
  44. ID_RGFX,ID_RGHD,
  45. ID_RGFX,ID_RSCM,
  46. ID_RGFX,ID_RCOL,
  47. ID_RGFX,ID_NAME,
  48. ID_RGFX,ID_AUTH,
  49. ID_RGFX,ID_ANNO,
  50. ID_RGFX,ID_Copyright,
  51. ID_RGFX,ID_FVER
  52. };
  53.  
  54. void ReadPrefs(Data *data)
  55. {
  56. char *var;
  57. strcpy(data->xpk,"NUKE");
  58. data->xpkmode=100;
  59. if(data->password)
  60.     {
  61.     FreeVec(data->password);
  62.     data->password=0;
  63.     }
  64. if(var=(char *)AllocVec(256,0))
  65.     {
  66.     if(GetVar("ENV:DataTypes/rgfx.prefs",var,256,LV_VAR|GVF_GLOBAL_ONLY)>=0)
  67.         {
  68.         RDArgs *rdargs;
  69.       if(rdargs=(RDArgs *)AllocDosObject(DOS_RDARGS,0))
  70.             {
  71.             RDArgs *args;
  72.             Arg1 para;
  73.             rdargs->RDA_Source.CS_Buffer=var;
  74.             rdargs->RDA_Source.CS_Length=strlen(var);
  75.             rdargs->RDA_Source.CS_CurChr=0;
  76.             memset(¶,0,sizeof(Arg1));
  77.             if(args=ReadArgs("XPK/A,XPKMODE/A/N,PASSWORD",(long *)¶,rdargs))
  78.                 {
  79.                 strncpy(data->xpk,para.xpk,4);
  80.                 data->xpk[4]=0;
  81.                 data->xpkmode=*para.xpkmode;
  82.                 if(data->xpkmode<0) data->xpkmode=0;
  83.                 else if(data->xpkmode>100) data->xpkmode=100;
  84.                 if(para.password)
  85.                     {
  86.                     if(data->password=(char *)AllocVec(strlen(para.password)+1,MEMF_CLEAR)) strcpy(data->password,para.password);
  87.                     }
  88.                 FreeArgs(args);
  89.                 }
  90.             FreeDosObject(DOS_RDARGS,rdargs);
  91.             }
  92.         }
  93.     FreeVec(var);
  94.     }
  95. }
  96.  
  97. ULONG Colors2DT(Object *obj,UBYTE *color)
  98. {
  99. UBYTE *cr;
  100. ULONG *cregs,i,num,error=0;
  101. GetDTAttrs(obj,PDTA_NumColors,&num,PDTA_ColorRegisters,&cr,PDTA_CRegs,&cregs,TAG_END);
  102. if(cr&&cregs)
  103.     {
  104.     for(i=0;i<num;i++,cr+=3,cregs+=3,color+=4)
  105.         {
  106.         cr[0]=color[1];
  107.         cr[1]=color[2];
  108.         cr[2]=color[3];
  109.         cregs[0]=cr[0]<<24;
  110.         cregs[1]=cr[1]<<24;
  111.         cregs[2]=cr[2]<<24;
  112.         }
  113.     }
  114. else error=ERROR_NO_FREE_STORE;
  115. return error;
  116. }
  117.  
  118. static void GetRGHD(Object *obj,RGHD *rh,BitMapHeader *bh,Data *data)
  119. {
  120. bh->bmh_Left=rh->rgfx_LeftEdge;
  121. bh->bmh_Top=rh->rgfx_TopEdge;
  122. bh->bmh_Width=rh->rgfx_Width;
  123. bh->bmh_Height=rh->rgfx_Height;
  124. bh->bmh_PageWidth=rh->rgfx_PageWidth?rh->rgfx_PageWidth:bh->bmh_Width;
  125. bh->bmh_PageHeight=rh->rgfx_PageHeight?rh->rgfx_PageHeight:bh->bmh_Height;
  126. bh->bmh_Depth=rh->rgfx_Depth;
  127. bh->bmh_Compression=cmpByteRun1;
  128. bh->bmh_XAspect=rh->rgfx_XAspect;
  129. bh->bmh_YAspect=rh->rgfx_YAspect;
  130. }
  131.  
  132. static ULONG GetRSCM(Object *obj,IFFHandle *iff,BitMapHeader *bh,Data *data,ULONG rgfxdepth)
  133. {
  134. ULONG mode=0,numc=0;
  135. StoredProperty *sp;
  136. mode=INVALID_ID;
  137. if(sp=FindProp(iff,ID_RGFX,ID_RSCM))
  138.     {
  139.     RSCM *rscm=(RSCM *)sp->sp_Data;
  140.     if(!ModeNotAvailable(rscm->rscm_AGA))
  141.         {
  142.         ULONG dipf=0;
  143.         if(rscm->rscm_AGA&HAM_KEY)
  144.             {
  145.             numc=(bh->bmh_Depth==8)?64:16;
  146.             dipf=DIPF_IS_HAM;
  147.             }
  148.         else if(rscm->rscm_AGA&EXTRAHALFBRITE_KEY)
  149.             {
  150.             numc=32;
  151.             dipf=DIPF_IS_EXTRAHALFBRITE;
  152.             }
  153.         if(dipf) mode=BestModeID(
  154.         BIDTAG_NominalWidth,bh->bmh_PageWidth,BIDTAG_NominalHeight,bh->bmh_PageHeight,
  155.         BIDTAG_DesiredWidth,bh->bmh_Width,BIDTAG_DesiredHeight,bh->bmh_Height,BIDTAG_Depth,bh->bmh_Depth,
  156.         BIDTAG_DIPFMustHave,dipf,BIDTAG_DIPFMustNotHave,DIPF_IS_DUALPF|DIPF_IS_PF2PRI,TAG_END);
  157.         }
  158.     }
  159. if(mode==INVALID_ID)
  160.     {
  161.     mode=BestModeID(
  162.     BIDTAG_NominalWidth,bh->bmh_PageWidth,BIDTAG_NominalHeight,bh->bmh_PageHeight,
  163.     BIDTAG_DesiredWidth,bh->bmh_Width,BIDTAG_DesiredHeight,bh->bmh_Height,
  164.     BIDTAG_Depth,bh->bmh_Depth,
  165.     BIDTAG_DIPFMustNotHave,DIPF_IS_DUALPF|DIPF_IS_PF2PRI|DIPF_IS_HAM|DIPF_IS_EXTRAHALFBRITE,TAG_END);
  166.     numc=1<<bh->bmh_Depth;
  167.     }
  168. if(mode!=INVALID_ID)
  169.     {
  170.     SetDTAttrs(obj,0,0,PDTA_ModeID,mode,TAG_END);
  171.     if(numc) SetDTAttrs(obj,0,0,PDTA_NumColors,numc,TAG_END);
  172.     }
  173. return mode;
  174. }
  175.  
  176. static ULONG GetRCOL(Object *obj,IFFHandle *iff,RGHD *rh)
  177. {
  178. ULONG error=0;
  179. if(rh->rgfx_Depth<=8)
  180.     {
  181.     StoredProperty *sp;
  182.     if(sp=FindProp(iff,ID_RGFX,ID_RCOL))
  183.         {
  184.         RCOL *rcol=(RCOL *)sp->sp_Data;
  185.         ULONG n,*cregs;
  186.         UBYTE *cr,*p;
  187.         GetDTAttrs(obj,PDTA_NumColors,&n,PDTA_ColorRegisters,&cr,PDTA_CRegs,&cregs,TAG_END);
  188.         if(cr&&cregs)
  189.             {
  190.             ULONG i;
  191.             n*=3;
  192.             for(i=0,p=rcol->rcol_Colors[0];i<n;i++,p++,cr++,cregs++)
  193.                 {
  194.                 *cr=*p;
  195.                 *cregs=(*p)<<24;
  196.                 }
  197.             }
  198.         else error=ERROR_NO_FREE_STORE;
  199.         }
  200.     else error=ERROR_REQUIRED_ARG_MISSING;
  201.     }
  202. return error;
  203. }
  204.  
  205. static ULONG GetBody_BP8(IFFHandle *iff,RGHD *rh,BitMap *bm,Data *data)
  206. {
  207. ULONG error=0,d;
  208. for(d=0;d<rh->rgfx_Depth;d++)
  209.     {
  210.     ULONG y;
  211.     UBYTE *pl=bm->Planes[d];
  212.     for(y=0;y<rh->rgfx_Height;y++,pl+=bm->BytesPerRow)
  213.         {
  214.         long err;
  215.         if((err=ReadChunkBytes(iff,pl,rh->rgfx_BytesPerLine)<0))
  216.             {
  217.             error=iff2dos[-err];
  218.             break;
  219.             }
  220.         }
  221.     }
  222. return error;
  223. }
  224.  
  225. static ULONG GetBody_BC8(IFFHandle *iff,RGHD *rh,BitMap *bm,Data *data)
  226. {
  227. ULONG error=0;
  228. long err;
  229. UBYTE *pix;
  230. RastPort rp;
  231. if(pix=(UBYTE *)AllocVec(rh->rgfx_Width*rh->rgfx_Height,0))
  232.     {
  233.     if((err=ReadChunkBytes(iff,pix,rh->rgfx_Width*rh->rgfx_Height))<0) error=iff2dos[-err];
  234.     else
  235.         {
  236.         InitRastPort(&rp);
  237.         rp.BitMap=bm;
  238.         WriteChunkyPixels(&rp,0,0,rh->rgfx_Width-1,rh->rgfx_Height-1,pix,rh->rgfx_Width);
  239.         }
  240.     FreeVec(pix);
  241.     }
  242. else
  243.     {
  244.     if(pix=(UBYTE *)AllocVec(rh->rgfx_Width,0))
  245.         {
  246.         ULONG y;
  247.         InitRastPort(&rp);
  248.         rp.BitMap=bm;
  249.         for(y=0;y<rh->rgfx_Height;y++)
  250.             {
  251.             long err;
  252.             if((err=ReadChunkBytes(iff,pix,rh->rgfx_Width))<0)
  253.                 {
  254.                 error=iff2dos[-err];
  255.                 break;
  256.                 }
  257.             WriteChunkyPixels(&rp,0,y,rh->rgfx_Width-1,1,pix,rh->rgfx_Width);
  258.             }
  259.         FreeVec(pix);
  260.         }
  261.     else error=ERROR_NO_FREE_STORE;
  262.     }
  263. return error;
  264. }
  265.  
  266. static ULONG GetBody_RGB_V43(Object *obj,IFFHandle *iff,RGHD *rh,Data *data)
  267. {
  268. ULONG error=0;
  269. UBYTE *pix;
  270. long err;
  271. if(pix=(UBYTE *)AllocVec(rh->rgfx_BytesPerLine*rh->rgfx_Height,0))
  272.     {
  273.     if((err=ReadChunkBytes(iff,pix,rh->rgfx_BytesPerLine*rh->rgfx_Height))<0) error=iff2dos[-err];
  274.     else DoMethod(obj,PDTM_WRITEPIXELARRAY,pix,PBPAFMT_RGB,rh->rgfx_BytesPerLine,0,0,rh->rgfx_Width,rh->rgfx_Height);
  275.     FreeVec(pix);
  276.     }
  277. else if(pix=(UBYTE *)AllocVec(rh->rgfx_BytesPerLine,0))
  278.     {
  279.     ULONG y;
  280.     for(y=0;y<rh->rgfx_Height;y++)
  281.         {
  282.         if((err=ReadChunkBytes(iff,pix,rh->rgfx_BytesPerLine))<0)
  283.             {
  284.             error=iff2dos[-err];
  285.             break;
  286.             }
  287.         DoMethod(obj,PDTM_WRITEPIXELARRAY,pix,PBPAFMT_RGB,rh->rgfx_BytesPerLine,0,y,rh->rgfx_Width,1);
  288.         }
  289.     FreeVec(pix);
  290.     }
  291. else error=ERROR_NO_FREE_STORE;
  292. return error;
  293. }
  294.  
  295. static ULONG GetBody_XPKBP8(IFFHandle *iff,RGHD *rh,BitMap *bm,Data *data)
  296. {
  297. ULONG error=0,d,y;
  298. XpkFib *fib;
  299. UBYTE *mem,*pl;
  300. if(!XpkUnpackTags(XPK_InFH,iff->iff_Stream,XPK_GetOutBuf,&mem,XPK_GetOutBufLen,&y,TAG_END))
  301.     {
  302.     UBYTE *p=mem;
  303.     for(d=0;d<rh->rgfx_Depth;d++)
  304.         {
  305.         pl=bm->Planes[d];
  306.         for(y=0;y<rh->rgfx_Height;y++,pl+=bm->BytesPerRow,p+=rh->rgfx_BytesPerLine) CopyMem(p,pl,bm->BytesPerRow);
  307.         }
  308.     FreeMem(mem,y);
  309.     }
  310. else if(!XpkOpenTags(&fib,XPK_InFH,iff->iff_Stream,TAG_END))
  311.     {
  312.     if(mem=(UBYTE *)AllocVec(fib->xf_NLen,0))
  313.         {
  314.         long p=-1,clen=-1;
  315.         for(d=0;d<rh->rgfx_Depth;d++)
  316.             {
  317.             pl=bm->Planes[d];
  318.             for(y=0;y<rh->rgfx_Height;y++,pl+=bm->BytesPerRow)
  319.                 {
  320.                 ULONG x;
  321.                 for(x=0;x<rh->rgfx_BytesPerLine;x++)
  322.                     {
  323.                     if(p>=clen)
  324.                         {
  325.                         if((clen=XpkRead(fib,mem,fib->xf_NLen))<0)
  326.                             {
  327.                             error=DTERROR_INVALID_DATA;
  328.                             y=rh->rgfx_Height;
  329.                             d=rh->rgfx_Depth;
  330.                             break;
  331.                             }
  332.                         p=0;
  333.                         }
  334.                     pl[x]=mem[p];
  335.                     p++;
  336.                     }
  337.                 }
  338.             }
  339.         FreeVec(mem);
  340.         }
  341.     else error=ERROR_NO_FREE_STORE;
  342.     XpkClose(fib);
  343.     }
  344. else error=DTERROR_INVALID_DATA;
  345. return error;
  346. }
  347.  
  348. static ULONG GetBody_XPKBC8(IFFHandle *iff,RGHD *rh,BitMap *bm,Data *data)
  349. {
  350. ULONG error=0,pixlen;
  351. UBYTE *pix;
  352. RastPort rp;
  353. if(!XpkUnpackTags(XPK_InFH,iff->iff_Stream,XPK_GetOutBuf,&pix,XPK_GetOutBufLen,&pixlen,TAG_END))
  354.     {
  355.     InitRastPort(&rp);
  356.     rp.BitMap=bm;
  357.     WriteChunkyPixels(&rp,0,0,rh->rgfx_Width-1,rh->rgfx_Height-1,pix,rh->rgfx_Width);
  358.     FreeMem(pix,pixlen);
  359.     }
  360. else
  361.